package com.business.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.system.util.DBTools;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.MapDAO;
import net.sf.rose.jdbc.query.MapSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.utils.WebUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.business.system.util.PageUtil;

/**
 * Created by jiangwenfeng on 2018/4/19.
 */
@RestController
@RequestMapping("rebate")
public class RebateAction {
    /**
     * 代理返利列表
     * @param service
     * @param request
     * @return
     * jiangwenfeng 2018-4-19 22:08
     */
    @RequestMapping("getAgentRebateList.do")
    public PageBean getAgentRebateList(Service service, HttpServletRequest request) {
        PageBean page = PageUtil.page(request);
        Map<String, Object> params = WebUtils.getRequestData(request);
        StringBuilder sql = new StringBuilder();
        sql.append("select t1.*,t2.USER_NAME as name,t3.USER_NAME as formName,t4.* from GOODS_ORDERS t1 left join wx_user t2 on t1.USER_ID=t2.ID")
                .append(" left join wx_user t3 on t2.FROM_ID = t3.ID")
                .append(" left join BACK_FEE_LOG t4 on t1.ID = t4.RESOURCE_ID where 1=1 AND t2.FROM_ID<>''");
        List<Object> condition = new ArrayList<>();
        if(params.containsKey("beginTime")){
            sql.append(" and t1.ORDER_TIME>=?");
            condition.add(params.get("beginTime"));
        }
        if(params.containsKey("endTime")){
            sql.append(" and t1.ORDER_TIME<=?");
            condition.add(params.get("endTime"));
        }
        if(params.containsKey("state") && !"0".equals(params.get("state"))){
            sql.append(" and t1.STATUS=?");
            condition.add(params.get("state"));
        }
        if(params.containsKey("content")){
            sql.append(" and t3.USER_NAME like '%"+params.get("content")+"%'");
        }
        sql.append(" order by t1.ORDER_TIME desc");

        MapDAO dao = new MapDAO(service);
        MapSQL query = dao.getQuerySQL();
        query.setSQL(sql.toString());
        query.setParameters(condition);
        query.setPage(page);
        List<Map<String, Object>> dataList = dao.list();
        DBTools.getDataList(service, sql.toString(), page,condition);
//        if(dataList.size()>0) {
//            for (Map<String, Object> data : dataList) {
//                String orderId = data.get("orderId").toString();
//                StringBuilder sql1 = new StringBuilder();
//                sql1.append("SELECT sum(t1.SALE_PRICE*t1.COUNTS*t3.AGENT_RATE) as sum FROM order_goods t1 LEFT JOIN goods t2 on t1.GOODS_ID=t2.ID")
//                        .append(" LEFT JOIN brand t3 on t2.BRAND_ID=t3.ID WHERE t1.ORDERS_ID=?");
//                query.setSQL(sql1.toString());
//                query.addParameter(orderId);
//                Map<String, Object> sum = dao.get();
//                double rebateSum = Double.parseDouble(sum.get("sum").toString());
//                data.put("rebateSum", rebateSum);
//            }
//        }
        return page;

    }

    /**
     * 推荐返利列表
     * @param service
     * @param request
     * @return
     * jiangwenfeng 2018-4-20 21:58
     */
    @RequestMapping("getRecommendRebateList.do")
    public PageBean getRecommendRebateList(Service service, HttpServletRequest request) {
        PageBean page = PageUtil.page(request);
        Map<String, Object> params = WebUtils.getRequestData(request);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT t1.CREATE_TIME as applyTime,t1.UPDATE_TIME as payTime,t1.TYPE as applyType,t1.*,t2.USER_NAME as name,t3.USER_NAME as formName,t4.*")
                .append(" FROM apply t1 LEFT JOIN wx_user t2 on t1.USER_ID = t2.ID")
                .append(" LEFT JOIN wx_user t3 on t2.FROM_ID = t3.ID")
                .append(" left join BACK_FEE_LOG t4 on t1.ID = t4.RESOURCE_ID where 1=1 AND t2.FROM_ID<>''");
        List<Object> condition = new ArrayList<>();
        if(params.containsKey("beginTime")){
            sql.append(" and t1.CREATE_TIME>=?");
            condition.add(params.get("beginTime"));
        }
        if(params.containsKey("endTime")){
            sql.append(" and t1.CREATE_TIME<=?");
            condition.add(params.get("endTime"));
        }
        if(params.containsKey("state") && !"0".equals(params.get("state"))){
                if("1".equals(params.get("state").toString())){
                    sql.append(" and t1.APPLY_STATUS=1");
                }else if("2".equals(params.get("state").toString())){
                    sql.append(" and t1.APPLY_STATUS=2 and t1.PAY_TYPE is null");
                }else if("3".equals(params.get("state").toString())){
                    sql.append(" and t1.APPLY_STATUS=3");
                }else if("4".equals(params.get("state").toString())){
                    sql.append(" and t1.PAY_STATUS=1");
                }else if("5".equals(params.get("state").toString())){
                    sql.append(" and t1.PAY_STATUS=2");
                }else if("6".equals(params.get("state").toString())){
                    sql.append(" and t1.PAY_STATUS=3");
                }else if("7".equals(params.get("state").toString())){
                    sql.append(" and t1.PAY_STATUS=4");
                }

        }
        if(params.containsKey("content")){
            sql.append(" and t3.USER_NAME like '%"+params.get("content")+"%'");
        }
        sql.append(" order by t1.CREATE_TIME desc");
        DBTools.getDataList(service, sql.toString(), page,condition);
        return page;
    }
}
